***************************************************************************************
*** Replication code for "State Repression and Opposition Survival in Pinochet's Chile"
*** by Consuelo Amat 
*** CPS, January 2023
*********************

*** APPENDIX ***

** Software and version: STATA 17.0

** Load Data
clear all
cd "~/directory" // fill in
insheet using "sequence_captures_only_targeted.csv"

**Install programs
ssc install rangestat


*Adding survival model structure to data:
gen date_mdy = mdy(month_detention, day_detention, year_detention)
gen date_mdy2 = mdy(month_detention, day_detention, year_detention)
format date_mdy2 %td
stset date_mdy 

*Calculate average number of captures in a 30-day period:

gen obs = .
replace obs = 1 if orgid!=.

sort year_detention month_detention day_detention

rangestat (sum) sum_speed_detention=obs, interval(date_mdy2 0 30) by(orgid) 
gen speed_detention=.
replace speed_detention= sum_speed_detention/30 


*Create variables to compare MIR to PS, and MIR to PC:
gen mir_vs_ps = .
replace mir_vs_ps=1 if orgid==101
replace mir_vs_ps=0 if orgid==132

gen mir_vs_pc = .
replace mir_vs_pc=1 if orgid==101
replace mir_vs_pc=0 if orgid==131


*Figure 11

*Plot:
twoway (histogram date_mdy2 if date_mdy>5001 & date_mdy<6199, freq discrete by(mir_vs_ps) yaxis(1)) ///
	   (line speed_detention date_mdy2 if date_mdy>5001 & date_mdy<6199, by(mir_vs_ps) yaxis(2) lpattern(dash) lwidth(medium) lcolor(blue))   
	   

*Figure 12
twoway (histogram date_mdy2 if date_mdy>5001 & date_mdy<6199, freq discrete by(mir_vs_pc) yaxis(1)) ///
	   (line speed_detention date_mdy2 if date_mdy>5001 & date_mdy<6199, by(mir_vs_pc) yaxis(2) lpattern(dash) lwidth(medium) lcolor(blue)) 



*Figure 13
**OLS models for plot:
*Comparing MIR vs. PS and PC: Sept 11, 1973-1976, inclusive
regress speed_detention mir region_n if (date_mdy>5001 & date_mdy<6199)  
estimates store Sept1173_Dec76MIR
*Comparing MIR vs. PS and PC: whole period 
regress speed_detention mir region_n 
estimates store wholeperiodMIR
*Comparing MIR vs. PS: Sept 11, 1973-1976, inclusive 
regress speed_detention mir_vs_ps region_n if (date_mdy>5001 & date_mdy<6199) 
estimates store Sept1173_Dec76MIRPS
*Comparing MIR vs. PS: whole period 
regress speed_detention mir_vs_ps region_n 
estimates store wholeperiodMIRPS
*Comparing MIR vs. PC: Sept 11, 1973-1976, inclusive 
regress speed_detention mir_vs_pc region_n if (date_mdy>5001 & date_mdy<6199) 
estimates store Sept1173_Dec76MIRPC
*Comparing MIR vs. PC: whole period 
regress speed_detention mir_vs_pc region_n 
estimates store wholeperiodMIRPC

*Plot main coefficients from all models:
coefplot Sept1173_Dec76MIR wholeperiodMIR Sept1173_Dec76MIRPS wholeperiodMIRPS Sept1173_Dec76MIRPC wholeperiodMIRPC, horizontal xline(0) drop(_cons)
